package org.timeismoney.payment.web.action.teacher;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.timeismoney.payment.common.PageBean;
import org.timeismoney.payment.model.Unit;
import org.timeismoney.payment.model.UnitLevel;
import org.timeismoney.payment.service.UnitLevelService;
import org.timeismoney.payment.service.UnitService;
import org.timeismoney.payment.web.WebUtils;
import org.timeismoney.payment.web.action.BaseAction;
import org.timeismoney.payment.web.form.UnitForm;

public class AdminUnitAction extends BaseAction {
	private static Logger		log	= Logger.getLogger(AdminUnitAction.class);
	private UnitService	service;
	private UnitLevelService levelService;

	public void setLevelService(UnitLevelService levelService) {
		this.levelService = levelService;
	}

	public void setService(UnitService service) {
		this.service = service;
	}

	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//rows=10&page=1
		//sidx	schoolYearNo
		//sord	desc
		String rows = request.getParameter("rows");
		String page = request.getParameter("page");
		String sortCol = request.getParameter("sidx");
		String sortD = request.getParameter("sord");
		int rowsNo = 10;
		try {
			rowsNo = Integer.valueOf(rows);
		} catch (RuntimeException e) {}
		int pageNo = 1;
		try {
			pageNo = Integer.valueOf(page);
		} catch (RuntimeException e) {}
		UnitForm unitForm = (UnitForm) form;
		Unit unit = new Unit();
		if(unitForm.getParentUnitNo()!=null&&unitForm.getParentUnitNo()!=0){
			Unit parent = new Unit();
			parent.setUnitNo(unitForm.getParentUnitNo());
			unit.setParentUnit(parent);
		}
		if(unitForm.getUnitLevelNo()!=null&&unitForm.getUnitLevelNo()!=0){
			UnitLevel level = new UnitLevel();
			level.setLevelNo(unitForm.getUnitLevelNo());
			unit.setUnitLevel(level);
		}
		PageBean<Unit> pageBean = new PageBean<Unit>(pageNo,rowsNo,sortCol,sortD);
		service.findUnitByParentUnitOrLevel(unit, pageBean);
		renderJson(response, WebUtils.generateJSONByPageBean(pageBean));
		return null;
	}
	
	public ActionForward get(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		try {
			UnitForm unitForm = (UnitForm) form;
			Unit unit = new Unit();
			if(unitForm.getParentUnitNo()!=null&&unitForm.getParentUnitNo()!=0){
				Unit parent = new Unit();
				parent.setUnitNo(unitForm.getParentUnitNo());
				unit.setParentUnit(parent);
			}
			if(unitForm.getUnitLevelNo()!=null&&unitForm.getUnitLevelNo()!=0){
				UnitLevel level = new UnitLevel();
				level.setLevelNo(unitForm.getUnitLevelNo());
				unit.setUnitLevel(level);
			}
			List<Unit> units = service.findUnitByParentUnitOrLevel(unit);
			renderJson(response, WebUtils.generateJSONByCollection(units));
		} catch (Exception e) {
			log.error(e.getMessage());
			renderJson(response, WebUtils.generateErrorJSON(TEXT_ERROR_MSG_QUERY+" "+e.getMessage()));
		}
		return null;
	} 

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		try {
			UnitForm unitForm = (UnitForm) form;
			Unit unit = new Unit();
			if(unitForm.getParentUnitNo()!=null&&unitForm.getParentUnitNo()!=0){
				Unit parent = service.get(unitForm.getParentUnitNo());
				unit.setParentUnit(parent);
			}
			UnitLevel level = levelService.get(unitForm.getUnitLevelNo());
			unit.setUnitLevel(level);
			unit.setUnitName(unitForm.getUnitName());
			service.save(unit);
		} catch (Exception e) {
			log.error(e.getMessage());
			renderJson(response, WebUtils.generateErrorJSON(TEXT_ERROR_MSG_ADD+" "+e.getMessage()));
		}
		return null;
	}

	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		try {
			UnitForm unitForm = (UnitForm) form;
			Unit unit = service.get(unitForm.getUnitNo());
			unit.setUnitName(unitForm.getUnitName());
			service.update(unit);
		} catch (Exception e) {
			log.error(e.getMessage());
			renderJson(response, WebUtils.generateErrorJSON(TEXT_ERROR_MSG_EDIT+" "+e.getMessage()));
		}
		return null;
	}

	public ActionForward del(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		try {
			UnitForm unitForm = (UnitForm) form;
			Unit unit = service.get(unitForm.getUnitNo());
			service.delete(unit);
		} catch (Exception e) {
			log.error(e.getMessage());
			renderJson(response, WebUtils.generateErrorJSON(TEXT_ERROR_MSG_DEL+" "+e.getMessage()));
		}
		return null;
	}


}
